package com.gqj.meter.controller;

import com.alibaba.fastjson.JSON;
import com.gqj.common.annotation.Log;
import com.gqj.common.constant.UserConstants;
import com.gqj.common.core.controller.BaseController;
import com.gqj.common.core.domain.AjaxResult;
import com.gqj.common.core.page.TableDataInfo;
import com.gqj.common.enums.BusinessType;
import com.gqj.common.utils.StorageUtil;
import com.gqj.common.utils.StringUtils;
import com.gqj.common.utils.poi.ExcelUtil;
import com.gqj.meter.domain.MdConcentrator;
import com.gqj.meter.domain.MdInTaskDetails;
import com.gqj.meter.domain.MdOtherMaterial;
import com.gqj.meter.domain.vo.MdOtherMaterialVo;
import com.gqj.meter.mapper.MdInTaskDetailsMapper;
import com.gqj.meter.service.IMdOtherMaterialService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 其他物资Controller
 *
 * @author lxq
 * @date lxq-02-10
 */
@Api(value = "/meter/mdOtherMaterial", description = "其他物资")
@RestController
@RequestMapping("/meter/mdOtherMaterial")
public class MdOtherMaterialController extends BaseController {
    @Autowired
    private IMdOtherMaterialService mdOtherMaterialService;
    @Value("${downloadPath}")
    private String downloadPath;
    @Autowired
    private MdInTaskDetailsMapper mdInTaskDetailsMapper;

    /**
     * 查询其他物资列表
     */
    @ApiOperation("查询其他物资列表")
    @PreAuthorize("@ss.hasPermi('meter:mdOtherMaterial:list')")
    @GetMapping("/list")
    public TableDataInfo list(MdOtherMaterial mdOtherMaterial) {
        startPage();
        List<MdOtherMaterial> list = mdOtherMaterialService.selectMdOtherMaterialList(mdOtherMaterial);
        return getDataTable(list);
    }

    /**
     * 导出其他物资列表
     */
    @ApiOperation("导出其他物资列表")
    @PreAuthorize("@ss.hasPermi('meter:mdOtherMaterial:export')")
    @Log(title = "其他物资", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, MdOtherMaterial mdOtherMaterial) {
        List<MdOtherMaterial> list = mdOtherMaterialService.selectMdOtherMaterialList(mdOtherMaterial);
        ExcelUtil<MdOtherMaterial> util = new ExcelUtil<MdOtherMaterial>(MdOtherMaterial.class);
        util.exportExcel(response, list, "其他物资数据");
    }

    /**
     * 获取其他物资详细信息
     */
    @ApiOperation("获取其他物资详细信息")
    @PreAuthorize("@ss.hasPermi('meter:mdOtherMaterial:query')")
    @GetMapping(value = "/{otherMaterialId}")
    public AjaxResult getInfo(@PathVariable("otherMaterialId") Long otherMaterialId) {
        return success(mdOtherMaterialService.selectMdOtherMaterialByOtherMaterialId(otherMaterialId));
    }

    /**
     * 新增其他物资
     */
    @ApiOperation("新增其他物资")
    @PreAuthorize("@ss.hasPermi('meter:mdOtherMaterial:add')")
    @Log(title = "其他物资", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MdOtherMaterial mdOtherMaterial) {

        if (UserConstants.NOT_UNIQUE.equals(mdOtherMaterialService.checkBarCodeUnique(mdOtherMaterial))) {
            return AjaxResult.error("新增'" + mdOtherMaterial.getBarCode() + "'失败，设备条形码已存在！");
        }
        if (StringUtils.isEmpty(mdOtherMaterial.getBarCode())) {
            return AjaxResult.error("条形码为空，请检查后重试");
        }
        return toAjax(mdOtherMaterialService.insertMdOtherMaterial(mdOtherMaterial));
    }

    /**
     * 修改其他物资
     */
    @ApiOperation("修改其他物资")
    @PreAuthorize("@ss.hasPermi('meter:mdOtherMaterial:edit')")
    @Log(title = "其他物资", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MdOtherMaterial mdOtherMaterial) {
        if (null == mdOtherMaterial.getOtherMaterialId() || 0 == mdOtherMaterial.getOtherMaterialId()) {
            return AjaxResult.error("修改失败，其他物资ID为空！");
        }
        if (UserConstants.NOT_UNIQUE.equals(mdOtherMaterialService.checkBarCodeUnique(mdOtherMaterial))) {
            return AjaxResult.error("新增'" + mdOtherMaterial.getBarCode() + "'失败，设备条形码已存在！");
        }
        return toAjax(mdOtherMaterialService.updateMdOtherMaterial(mdOtherMaterial));
    }

    /**
     * 删除其他物资
     */
    @ApiOperation("删除其他物资")
    @PreAuthorize("@ss.hasPermi('meter:mdOtherMaterial:remove')")
    @Log(title = "其他物资", businessType = BusinessType.DELETE)
    @DeleteMapping("/{otherMaterialIds}")
    public AjaxResult remove(@PathVariable Long[] otherMaterialIds) {
        //判断集中器是否被引用
        for (int i = 0; i < otherMaterialIds.length; i++) {
            MdOtherMaterial mdOtherMaterial = mdOtherMaterialService.selectMdOtherMaterialByOtherMaterialId(otherMaterialIds[i]);
            //查看盘点单是否引用
            String serviceStatus = mdOtherMaterial.getServiceStatus();
            if (!"00".equals(serviceStatus) && !"01".equals(serviceStatus) && !"04".equals(serviceStatus)) {
                return AjaxResult.error("其他物资" + mdOtherMaterial.getBarCode() + "的业务状态不允许删除！");
            }

            //查看出入库是否引用
            MdInTaskDetails mdInTaskDetails = new MdInTaskDetails();
            mdInTaskDetails.setRfidNo(mdOtherMaterial.getBarCode());
            mdInTaskDetails.setIsComplete("0");
            List<MdInTaskDetails> mdInTaskDetailsList = mdInTaskDetailsMapper.selectMdInTaskDetailsList(mdInTaskDetails);
            if (mdInTaskDetailsList.size() > 0) {
                return AjaxResult.error("其他物资" + mdOtherMaterial.getBarCode() + "入库任务中已经引用，无法删除！");
            }
        }
        return toAjax(mdOtherMaterialService.deleteMdOtherMaterialByOtherMaterialIds(otherMaterialIds));
    }

    /**
     * 下载其他物资json文件
     */
    @ApiOperation("下载其他物资json文件")
    @PreAuthorize("@ss.hasPermi('meter:mdOtherMaterial:downloadJsonMdOtherMaterial')")
    @GetMapping("/downloadJsonMdOtherMaterial")
    public AjaxResult downloadJsonMdOtherMaterial(MdOtherMaterial mdOtherMaterial) {
        List<MdOtherMaterial> om = mdOtherMaterialService.selectMdOtherMaterialList(mdOtherMaterial);
        List<MdOtherMaterialVo> vos = StorageUtil.copyList(om, MdOtherMaterialVo::new);

        String orderNo = StorageUtil.getOrderNoTo12();

        String fileName = downloadPath;
        String jsonStringAc = JSON.toJSONString(vos);
        StorageUtil.createJsonFile(jsonStringAc, fileName, "其他物资数据" + orderNo);
        return AjaxResult.success(fileName + "其他物资数据" + orderNo);
    }

    /**
     * 导入其他物资execl文件
     */
    @ApiOperation("导入其他物资execl文件")
    @Log(title = "导入其他物资execl文件", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('meter:mdOtherMaterial:importDataOtherMaterial')")
    @PostMapping("/importDataOtherMaterial")
    public AjaxResult importDataOtherMaterial(MultipartFile file, boolean updateSupport) throws Exception {
        ExcelUtil<MdOtherMaterial> util = new ExcelUtil<MdOtherMaterial>(MdOtherMaterial.class);
        List<MdOtherMaterial> mdOtherMaterialList = util.importExcel(file.getInputStream());
        String operName = getUsername();
        String message = mdOtherMaterialService.importOtherMaterial(mdOtherMaterialList, updateSupport, operName);
        return success(message);
    }

    @ApiOperation("下载其他物资模板")
    @PostMapping("/importTemplateMdOtherMaterial")
    @PreAuthorize("@ss.hasPermi('meter:mdOtherMaterial:importTemplateMdOtherMaterial')")
    public void importTemplateMdOtherMaterial(HttpServletResponse response) {
        ExcelUtil<MdOtherMaterial> util = new ExcelUtil<MdOtherMaterial>(MdOtherMaterial.class);
        util.importTemplateExcel(response, "其他物资数据");
    }
}
